<?php
/**
 * Staff management
 * @author Ice <iceinto@mallog.com.cn>
 */
class StaffAction extends CommonAction {
	public $gid = 4;
	public function _initialize() {
		$this->assign ('nav', 6);
		parent::_initialize();
		if($this->_level != 1){
			$this->errorLang('_VALID_ACCESS_');
		}
	}

	public function index(){
		$map = array();
		$map['fid'] = $this->getLoginUserInfo();
		$UserModel = D('User');
		$userCount = $UserModel->where($map)->count();
		$Page = $this->getPage($userCount);
		$users = $UserModel->getList($map, $Page->firstRow.','.$Page->listRows);
		$this->assign('page', $Page->show());
		$this->assign('users', $users);
		$this->display();
	}

	/**
	 * Add staff
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function add(){
		if(isPost()){
			$this->autoCheckToken();
			$UserModel = D('User');
			$data = $UserModel->create();
			if($data){
				$passwd = hh(getPost('passwd'));
				if($passwd){
					$data['passwd'] = md5($passwd);
				}else{
					$this->error('_PASSWD_MUST');
				}
				$data['fid'] = $this->getLoginUserInfo();
				$uid = $UserModel->add($data);
				if($uid){
					D('RoleUser')->setUserRloe($uid, 4);
					$this->success(L('_ADD_SUCCESS'));
				}else{
					$this->errorLang('_ADD_LOSE');
				}
			}else{
				$this->error($UserModel->getError());
			}
		}
		$this->display();
	}

	/**
	 * Update staff
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function update(){
		$id = intval(getGet('id'));
		if($id){
			$map = array();
			$map['fid'] = $this->getLoginUserInfo();
			$map['id'] = $id;
			$UserModel = M('User');
			$user = $UserModel->where($map)->find();
			if(!$user){
				$this->error(L('_VALID_ACCESS_'));
			}
			$this->assign('user', $user);
			//数据提交
			if(isPost()){
				$this->autoCheckToken();
				$UserModel = D('User');
				$data = $UserModel->create();
				if($data){
					$passwd = hh(getPost('passwd'));
					if($passwd){
						$data['passwd'] = md5($passwd);
					}else{
						unset($data['passwd']);
					}
					$data['id'] = $id;
					if($UserModel->save($data)){
						$this->success(L('_UPDATE_SUCCESS'));
					}else{
						$this->error(L('_UPDATE_LOSE'));
					}
				}else{
					$this->error($UserModel->getError());
				}
			}
		}else{
			$this->error(L('_USER_ID_ERROR'));
		}
		$this->display();
	}

	/**
	 * Staff level
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function management(){
		$PerModel = D('Per');
		$mid = $this->_mall[0]['mid'];
		$uid = intval(getGet('id'));
		if($uid){
			$map = array();
			$map['fid'] = $this->getLoginUserInfo();
			$map['id'] = $uid;
			$UserModel = M('User');
			$user = $UserModel->where($map)->find();
			if(!$user){
				$this->error(L('_VALID_ACCESS_'));
			}
			$this->assign('user', $user);
		}else{
			$this->error(L('_USER_ID_ERROR'));
		}
		if(isPost()){
			//$this->autoCheckToken();
			$level = intval(getPost('level'));
			if($level == 2 || $level == 3){
				$fid = intval(getPost('floor'));
				if(!$fid){
					$this->error('楼层信息错误');
				}
				//TODO 添加对楼层真实性判断
				//等级：1为企划部2楼层主管3品牌经理4柜台
				$data = array();
				$data['uid'] = $uid;
				$data['level'] = $level;
				$data['mid'] = $mid;
				$data['fid'] = $fid;
				$data['bids'] = 0;
				if($level == 3){
					$brandsPer = getPost('brands');
					if($brandsPer){
						//TODO 添加对品牌真实性判断
						$data['bids'] = implode(',', $brandsPer);
					}else{
						$this->errorLang('_UPDATE_LOSE');
					}
				}
			}else{
				$this->error(L('_VALID_ACCESS_'));
			}

			$rs = $PerModel->setUserPer($uid, $data);
			if($rs){
				$this->successLang('_UPDATE_SUCCESS');
			}else{
				$this->errorLang('_UPDATE_LOSE');
			}
		}
		$ucount = $PerModel->where(array('uid'=>$uid))->count();
		$Mall = D('Mall');
		$mall = $Mall->getMall($mid);
		$floors = $Mall->getFloorList($mid);
		$this->assign('mall', $mall);
		$this->assign('floors', $floors);
		$this->assign('uid', $uid);
		if($ucount){
			//获取per 详细
			$per = $PerModel->find($uid);
			$this->assign('per', $per);
		}
		$this->display();
	}

	/**
	 * Get floor brands
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function getBrands() {
		$brands = array();
		$status = 0;
		$uid = intval(getGet('uid'));
		$fid = intval(getGet('fid'));
		$UserModel = M('User');
		$PerModel = D('Per');
		$Mall = D('Mall');
		$map = array();
		$map['fid'] = $this->getLoginUserInfo();
		$map['id'] = $uid;
		$user = $UserModel->where($map)->find();
		if(!$user){
			$this->ajaxReturn ( L('_VALID_ACCESS_'), null, $status );
		}
		$brands = D('Mall')->getFloorBrandList($fid);
		//获取per
		$map = array();
		$map['uid'] = $uid;
		$map['fid'] = $fid;
		$brandPer = $PerModel->where($map)->find();
		if($brandPer){
			foreach($brands as $key=>$brand){
				$bids = explode(',',  $brandPer['bids']);
				if(in_array($brand['id'] ,$bids)){
					$brands[$key]['isPer'] = true;
				}else{
					$brands[$key]['isPer'] = false;
				}
			}
		}
		if($brands){
			$status = 1;
		}
		$this->assign('brands', $brands);
		$this->ajaxReturn ( $this->view->fetch (), null, $status );
	}

}
?>